<form action="index.php?r=demo/adddo" method="post">
    <div class="form-group">
        <label for="exampleInputUsername">Username</label>
        <input type="text" class="form-control" id="exampleInputUsername" placeholder="Username" name="username">
    </div>
    <div class="form-group">
        <label for="exampleInputPassword">Password</label>
        <input type="password" class="form-control" id="exampleInputPassword" placeholder="Password" name="userpwd">
    </div>
    <div class="form-group">
        <label for="exampleInputTel">Tel</label>
        <input type="text" class="form-control" id="exampleInputTel" placeholder="Tel" name="tel">
    </div>
    <div class="form-group">
        <label for="exampleInputEmail1">Email address</label>
        <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email" name="email">
        <input type="hidden" name="_csrf" value="<?php echo Yii::$app->request->csrfToken ?>">
    </div>

    <button type="submit" class="btn btn-default">注册</button>
</form>
<script src="jquery.js"></script>
<script>
    //正则判断用户名
    $(document).on("blur","#exampleInputUsername",function(){
        var username = $(this).val()
        var demo1=/^\w{2,5}$/
        if(!demo1.test(username)){
            alert("用户名长度不够或太长");
            return false;
        }else{
            alert("没问题");
            return false;
        }
    })

    //正则判断密码
    $(document).on("blur","#exampleInputPassword",function(){
        var userpwd = $(this).val()
        var demo2=/^\w{8,16}$/
        if(!demo2.test(userpwd)){
            alert("密码长度不够或太长");
            return false;
        }else{
            alert("没问题");
            return false;
        }
    })

    //正则判断手机号
    $(document).on("blur","#exampleInputTel",function(){
        var tel = $(this).val()
        var demo3=/^1[3578]\d{9}$/
        if(!demo3.test(tel)){
            alert("手机号格式错误");
            return false;
        }else{
            alert("没问题");
            return false;
        }
    })

    //正则判断邮箱
    $(document).on("blur","#exampleInputEmail1",function(){
        var email = $(this).val()
        var demo4=/^\w+@\w+\.(com|cn|net)$/
        if(!demo4.test(email)){
            alert("邮箱格式错误");
            return false;
        }else{
            alert("没问题");
            return false;
        }
    })
</script>
